package com.flycms.module.comment.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.constant.StatusConst;
import com.flycms.module.app.dao.AppDao;
import com.flycms.module.article.dao.ArticleDao;
import com.flycms.module.comment.dao.AnswersCollectDao;
import com.flycms.module.comment.dao.AnswersCountDao;
import com.flycms.module.comment.dao.ArticlesCollectDao;
import com.flycms.module.comment.dao.ArticlesCountDao;
import com.flycms.module.comment.dao.ContentRelDao;
import com.flycms.module.comment.dao.QuestionsCollectDao;
import com.flycms.module.comment.dao.QuestionsCountDao;
import com.flycms.module.comment.model.AnswersOptionCount;
import com.flycms.module.comment.model.Collects;
import com.flycms.module.comment.model.FlyContentType;
import com.flycms.module.comment.model.OptionCount;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.dao.QuestionsDao;
import com.flycms.module.qasystem.model.Answers;

/**
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年2月22日
 * @Date 2020年2月22日
 */

@Service
public class CollectsService {

    @Autowired
    protected QuestionsDao questionDao;
    
    @Autowired
    protected AnswersDao answerDao;
    
    @Autowired
    protected ContentRelDao contentRel;
    
    @Autowired
    protected ArticleDao articleDao;
    
    @Autowired
    protected AppDao appDao;
    
    @Autowired
    protected QuestionsCollectDao questionsCollectDao;
    
    @Autowired
    protected QuestionsCountDao questionsCountDao;
    
    @Autowired
    protected AnswersCollectDao answersCollectDao;
    
    @Autowired
    protected AnswersCountDao answersCountDao;
    
    @Autowired
    protected ArticlesCollectDao articlesCollectDao;
    
    @Autowired
    protected ArticlesCountDao articlesCountDao;
    
    @Transactional
    public OptionCount addCollect(Collects collect, FlyContentType type) throws Exception {
        int i = 0;
        switch (type) {
          case QUESTION:
              i = questionsCollectDao.addCollect(collect);
              break;
          case ANSWER:
              i = answersCollectDao.addCollect(collect);
              break;
          case ARTICLE:
              i = articlesCollectDao.addCollect(collect);
              break;
          default: break;
        }
        OptionCount o = null;
        if (i != 0) {
            o = recordCollectCount(collect, type);
        }
        return o;
    }
    
    public OptionCount recordCollectCount(Collects collect, FlyContentType type) {
        OptionCount c = null;
        int i = 0;
        switch (type) {
          case QUESTION:
              i = questionsCollectDao.queryContentCollectCount(collect.getContentId());
              c = questionsCountDao.queryCountById(collect.getContentId());
              if (c == null) {
                  c = new OptionCount(collect.getContentId(), 0, 0, i, 0, 0);
                  questionsCountDao.addCount(c);
              } else {
                  c.setCollectCount(i);
                  questionsCountDao.updateCount(c);
              }
              break;
          case ANSWER:
              i = answersCollectDao.queryContentCollectCount(collect.getContentId());
              AnswersOptionCount aoc = answersCountDao.queryCountById(collect.getContentId());
              Answers a = answerDao.queryAnswerById(collect.getContentId(), null);
              if (aoc == null) {
                  aoc = new AnswersOptionCount();
                  aoc.setUpCount(0);
                  aoc.setDownCount(0);
                  aoc.setId(a.getId());
                  aoc.setQuestionId(a.getQuestionId());
                  aoc.setCommentCount(0);
                  aoc.setCollectCount(i);
                  answersCountDao.addCount(aoc);
              } else {
                  aoc.setCollectCount(i);
                  answersCountDao.updateCount(aoc);
              }
              break;
          case ARTICLE:
              i = articlesCollectDao.queryContentCollectCount(collect.getContentId());
              c = articlesCountDao.queryCountById(collect.getContentId());
              if (c == null) {
                  c = new OptionCount(collect.getContentId(), 0, 0, i, 0, 0);
                  articlesCountDao.addCount(c);
              } else {
                  c.setCollectCount(i);
                  articlesCountDao.updateCount(c);
              }
              break;
          default: break;
        }
        return c;
    }
    
    @Transactional
    public OptionCount removeCollect(Collects collect, FlyContentType type) throws Exception {
        OptionCount c = null;
        int i = 0;
        switch (type) {
          case QUESTION:
              questionsCollectDao.removeCollect(collect);
              i = questionsCollectDao.queryContentCollectCount(collect.getContentId());
              c = questionsCountDao.queryCountById(collect.getContentId());
              if (c == null) {
                  c = new OptionCount(collect.getContentId(), 0, 0, i, 0, 0);
                  questionsCountDao.addCount(c);
              } else {
                  c.setCollectCount(i);
                  questionsCountDao.updateCount(c);
              }
              break;
          case ANSWER:
              answersCollectDao.removeCollect(collect);
              i = answersCollectDao.queryContentCollectCount(collect.getContentId());
              AnswersOptionCount aoc = answersCountDao.queryCountById(collect.getContentId());
//              Answers a = answerDao.queryAnswerById(collect.getContentId(), null);
              if (aoc == null) {
//               
              } else {
                  aoc.setCollectCount(i);
                  answersCountDao.updateCount(aoc);
              }
              break;
          case ARTICLE:
              articlesCollectDao.removeCollect(collect);
              i = articlesCollectDao.queryContentCollectCount(collect.getContentId());
              c = articlesCountDao.queryCountById(collect.getContentId());
              if (c == null) {
                  c = new OptionCount(collect.getContentId(), 0, 0, i, 0, 0);
                  articlesCountDao.addCount(c);
              } else {
                  c.setCollectCount(i);
                  articlesCountDao.updateCount(c);
              }
              break;
          default: break;
        }
        return c;
    }
    
    public List<Collects> getCollectList(Long userId, Long contentId,
            String orderby, String order, int pageIndex, int pageSize, FlyContentType type) throws Exception {
        //只有默认，不开放
        orderby = "create_time";
        order = "desc";
        List<Collects> list = new ArrayList<>();
        switch (type) {
          case QUESTION:
              list = questionsCollectDao.getCollectList(userId, contentId, orderby, order, pageIndex, pageSize);
              break;
          case ANSWER:
              list = answersCollectDao.getCollectList(userId, contentId, orderby, order, pageIndex, pageSize);
              break;
          case ARTICLE:
              list = articlesCollectDao.getCollectList(userId, contentId, orderby, order, pageIndex, pageSize);
              break;
          default:
              break;
        }
        return list;
    }
    
    public int getCollectCount(Long userId, Long contentId, FlyContentType type) throws Exception {
        int count = 0;
        switch (type) {
          case QUESTION:
              count = questionsCollectDao.getCollectCount(userId, contentId);
              break;
          case ANSWER:
              count = answersCollectDao.getCollectCount(userId, contentId);
              break;
          case ARTICLE:
              count = articlesCollectDao.getCollectCount(userId, contentId);
              break;
        default:
            break;
        }
        return count;
    }
    
    public Object queryContentObject(Long contentId, FlyContentType type) throws Exception {
        Object obj = null;
        Integer status = StatusConst.OPEN.value();
        switch (type) {
          case QUESTION:
              obj = questionDao.queryQuestionById(contentId, status);
              break;
          case ANSWER:
              obj = answerDao.queryAnswerById(contentId, status);
              break;
          case ARTICLE:
              obj = articleDao.findArticleById(contentId, 1);//1 正常状态
              break;
          case INDUSTRY_APP:
              obj = appDao.selectById(contentId);
          default: break;
        }
        return obj;
    }
    
    public Collects queryCollectedCollect(Long userId, Long contentId, FlyContentType type) throws Exception {
        Collects cl = null;
        switch (type) {
          case QUESTION:
              cl = questionsCollectDao.queryCollectedCollect(userId, contentId);
              break;
          case ANSWER:
              cl = answersCollectDao.queryCollectedCollect(userId, contentId);
              break;
          case ARTICLE:
              cl = articlesCollectDao.queryCollectedCollect(userId, contentId);
              break;
          default: break;
        }
        return cl;
    }
    
    public Collects queryCollectById(Long userId, Integer id, FlyContentType type) throws Exception {
        Collects cl = null;
        switch (type) {
          case QUESTION:
              cl = questionsCollectDao.queryCollectById(id, userId);
              break;
          case ANSWER:
              cl = answersCollectDao.queryCollectById(id, userId);
              break;
          case ARTICLE:
              cl = articlesCollectDao.queryCollectById(id, userId);
              break;
          default:
              break;
        }
        return cl;
    }
    
}
